package com.twu.biblioteca.service;

import com.twu.biblioteca.IUserDao;
import com.twu.biblioteca.modal.User;

public class UserService {
    private IUserDao userDao;
    private boolean loginFlag;
    private String loginedNumber;

    public boolean login(String userNumber, String password) {
        for (User user : userDao.getUsers()) {
            if (user.getNumber().equals(userNumber) && user.getPassword().equals(password)) {
                loginFlag = true;
                loginedNumber = user.getNumber();
                return true;
            }
        }
        loginFlag = false;
        return false;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public boolean isLogined() {
        return loginFlag;
    }

    public String showInformation() {
        User targetUser = null;
        for (User user : userDao.getUsers()) {
            if (user.getNumber().equals(loginedNumber)) {
                targetUser = user;
                break;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(targetUser.getName()).append("\t").append(targetUser.getEmail()).append("\t").append(targetUser.getPhone());
        return stringBuilder.toString();
    }
}
